package com.adopapa.rabbitdemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HelloReceiver extends SimpleMessageListenerContainer implements ChannelAwareMessageListener {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	
	@Autowired
	public HelloReceiver(ConnectionFactory connectionFactory, Queue queue) {
		this.setConnectionFactory(connectionFactory);
		this.setQueues(queue);
		this.setExposeListenerChannel(true);
		this.setMaxConcurrentConsumers(1);
		this.setConcurrentConsumers(1);
		this.setAcknowledgeMode(AcknowledgeMode.MANUAL);
		this.setMessageListener(this);
	}
	
	
	
	@Override
	public void onMessage(Message message, com.rabbitmq.client.Channel channel) throws Exception {
		byte[] body = message.getBody();
		System.out.println("消费端接收到消息 : " + new String(body));
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}




}
